from flask import current_app
from flask import render_template, jsonify
from flask import request

from info.model import Student, db
from . import tutor_blu


@tutor_blu.route('/info', methods=['POST'])
def info():
    name = request.form.get('name')
    mobile = request.form.get('mobile')
    address = request.form.get('address')
    grade = request.form.get('grade')
    subject = request.form.get('subject')
    character = request.form.get('character')
    if not all([name, mobile, address, grade, subject, character]):
        return jsonify(errno='4100', errmsg="参数有误")

    students = Student()
    students.name = name
    students.mobile = mobile
    students.address = address
    students.grade = grade
    students.subject = subject
    students.character = character

    try:
        db.session.add(students)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno='4101', errmsg="数据保存失败")

    return jsonify(errno='200', errmsg="OK")


@tutor_blu.route('/detail')
def detail():

    return render_template("tutor.html")